﻿<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>超市管理系统</title>
    <link rel="stylesheet" href="css/public.css"/>
    <link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<!--头部-->
<header class="publicHeader">
    <h1>超市管理系统</h1>

    <div class="publicHeaderR">
        <p><span>下午好！</span><span style="color: #fff21b"> Admin</span> , 欢迎你！</p>
        <a href="logout">退出</a>
    </div>
</header>
<!--时间-->
<section class="publicTime">
    <span id="time">2020年1月1日 11:11  星期一</span>
    <a href="#">温馨提示：为了能正常浏览，请使用高版本浏览器！（IE10+）</a>
</section>
<!--主体内容-->
<section class="publicMian ">
    <div class="left">
        <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
        <nav>
            <ul class="list">
                <li><a href="billList.html">账单管理</a></li>
                <li id="active"><a href="providerList.html">物品管理</a></li>
                <li><a href="userList.html">用户管理</a></li>
                <!-- <li><a href="password.html">密码修改</a></li> -->
                <li><a href="logout">退出系统</a></li>
            </ul>
        </nav>
    </div>
    <div class="right">
        <div class="location">
            <strong>你现在所在的位置是:</strong>
            <span>物品管理页面</span>
        </div>
        <div class="search">
            <span>物品名称：</span>
            <input id="search_name" type="text" placeholder="请输入物品的名称"/>
            <input id="search_btn" type="button" value="查询"/>
            <a href="providerAdd.html">添加物品</a>
        </div>
        <!--物品操作表格-->
        <table class="providerTable" cellpadding="0" cellspacing="0">
            <tr class="firstTr">
                <th width="10%">物品编号</th>
                <th width="20%">名称</th>
                <th width="10%">分类</th>
                <th width="10%">价钱</th>
                <th width="10%">库存</th>
                <th width="10%">创建时间</th>
                <th width="30%">操作</th>
            </tr>
            <tr>
                <td>PRO-CODE—001</td>
                <td>测试物品001</td>
                <td>韩露</td>
                <td>15918230478</td>
                <td>15918230478</td>
                <td>2020-11-12</td>
                <td>
                    <a href="providerView.html"><img src="img/read.png" alt="查看" title="查看"/></a>
                    <a href="providerUpdate.html"><img src="img/xiugai.png" alt="修改" title="修改"/></a>
                    <a href="#" class="removeProvider"><img src="img/schu.png" alt="删除" title="删除"/></a>
                </td>
            </tr>
            <tr>
                <td>PRO-CODE—001</td>
                <td>测试物品001</td>
                <td>韩露</td>
                <td>15918230478</td>
                <td>15918230478</td>
                <td>2020-11-12</td>
                <td>
                    <a href="providerView.html"><img src="img/read.png" alt="查看" title="查看"/></a>
                    <a href="providerUpdate.html"><img src="img/xiugai.png" alt="修改" title="修改"/></a>
                    <a href="#" class="removeProvider"><img src="img/schu.png" alt="删除" title="删除"/></a>
                </td>
            </tr>
        </table>

    </div>
</section>

<!--点击删除按钮后弹出的页面-->
<div class="zhezhao"></div>
<div class="remove" id="removeProv">
   <div class="removerChid">
       <h2>提示</h2>
       <div class="removeMain" >
           <p>你确定要删除该物品吗？</p>
           <a href="#" id="yes">确定</a>
           <a href="#" id="no">取消</a>
       </div>
   </div>
</div>


<footer class="footer">
</footer>

<script src="js/jquery.js"></script>
<script src="js/js.js"></script>
<script src="js/time.js"></script>
<script src="js/axios.min.js"></script>
<script>
    function start(){
        axios({
            url: 'snack',
            method: 'GET'
        }).then(result => {
            const list = result.data.data
            let str = `<tr class="firstTr">
                        <th width="10%">物品编号</th>
                        <th width="20%">名称</th>
                        <th width="10%">分类</th>
                        <th width="10%">价钱</th>
                        <th width="10%">库存</th>
                        <th width="10%">创建时间</th>
                        <th width="30%">操作</th>
                    </tr>`

            // 将 JSON 字符串解析为 JavaScript 对象或数组
            
            console.log(list)
            
            list.forEach(item => {
                const {id, name, categoryId, price, createTime,inventory} = item
                str += `
                <tr>
                    <td>${id}</td>
                    <td>${name}</td>
                    <td>${categoryId === 1 ? '零食' : '水果'}</td>
                    <td>${price}</td>
                    <td>${inventory}</td>
                    <td>${createTime}</td>
                    <td>
                        <a href="#" class="removeProvider"><img src="img/schu.png" alt="删除" title="删除" data-id='${id}' /></a>
                    </td>
                </tr>
                `
            })
            document.querySelector('.providerTable').innerHTML=str
            const ls = document.querySelectorAll('.removeProvider')
            for(let i = 0; i < ls.length; i++){
                ls[i].addEventListener('click', function(e){
                    console.log(e.target.dataset.id)

                    $('.zhezhao').css('display', 'block');
                    $('#removeProv').fadeIn();
                    document.querySelector('#yes').setAttribute('data-id', e.target.dataset.id)
                })
            }
        });
    }
    
    start();

    document.querySelector('#yes').addEventListener('click', function(e){
        $('.zhezhao').css('display', 'none');
        $('#removeProv').fadeOut();
        axios({
            url: 'snack/del',
            method: 'POST',
            params: {
                id: e.target.dataset.id
            }
        }).then(result => {
            alert(result.data.data)
            start();
        })
    });

    const search_btn = document.getElementById('search_btn');

    search_btn.addEventListener('click', function(){
        const content = document.getElementById('search_name').value
        console.log(content)
        axios({
            url: 'search',
            method: 'GET',
            params: {
                content
            }
        }).then(result => {
            const list = result.data.data
            let str = `<tr class="firstTr">
                        <th width="10%">物品编号</th>
                        <th width="20%">名称</th>
                        <th width="10%">分类</th>
                        <th width="10%">价钱</th>
                        <th width="10%">库存</th>
                        <th width="10%">创建时间</th>
                        <th width="30%">操作</th>
                    </tr>`

            // 将 JSON 字符串解析为 JavaScript 对象或数组
            
            console.log(list)
            
            list.forEach(item => {
                const {id, name, categoryId, price, createTime, inventory} = item
                str += `
                <tr>
                    <td>${id}</td>
                    <td>${name}</td>
                    <td>${categoryId === 1 ? '零食' : '水果'}</td>
                    <td>${price}</td>
                    <td>${inventory}</td>
                    <td>${createTime}</td>
                    <td>
                        <a href="#" class="removeProvider"><img src="img/schu.png" alt="删除" title="删除" data-id='${id}' /></a>
                    </td>
                </tr>
                `
            })
            document.querySelector('.providerTable').innerHTML=str
            const ls = document.querySelectorAll('.removeProvider')
            for(let i = 0; i < ls.length; i++){
                ls[i].addEventListener('click', function(e){
                    console.log(e.target.dataset.id)

                    $('.zhezhao').css('display', 'block');
                    $('#removeProv').fadeIn();
                    document.querySelector('#yes').setAttribute('data-id', e.target.dataset.id)
                })
            }
        })
    })

    document.getElementById('search_name').addEventListener('keydown', function(event){
        if (event.keyCode === 13 || event.key === 'Enter') {
            const content = document.getElementById('search_name').value
            console.log(content)
            axios({
                url: 'search',
                method: 'GET',
                params: {
                    content
                }
            }).then(result => {
                const list = result.data.data
                let str = `<tr class="firstTr">
                            <th width="10%">物品编号</th>
                            <th width="20%">名称</th>
                            <th width="10%">分类</th>
                            <th width="10%">价钱</th>
                            <th width="10%">库存</th>
                            <th width="10%">创建时间</th>
                            <th width="30%">操作</th>
                        </tr>`

                // 将 JSON 字符串解析为 JavaScript 对象或数组
                
                console.log(list)
                
                list.forEach(item => {
                    const {id, name, categoryId, price, createTime, inventory} = item
                    str += `
                <tr>
                    <td>${id}</td>
                    <td>${name}</td>
                    <td>${categoryId === 1 ? '零食' : '水果'}</td>
                    <td>${price}</td>
                    <td>${inventory}</td>
                    <td>${createTime}</td>
                    <td>
                        <a href="#" class="removeProvider"><img src="img/schu.png" alt="删除" title="删除" data-id='${id}' /></a>
                    </td>
                </tr>
                `
                })
                document.querySelector('.providerTable').innerHTML=str
                const ls = document.querySelectorAll('.removeProvider')
                for(let i = 0; i < ls.length; i++){
                    ls[i].addEventListener('click', function(e){
                        console.log(e.target.dataset.id)

                        $('.zhezhao').css('display', 'block');
                        $('#removeProv').fadeIn();
                        document.querySelector('#yes').setAttribute('data-id', e.target.dataset.id)
                    })
                }
            })
        }
    });
</script>

</body>
</html>